<%@ page import="java.util.List" %>
<%@ page import="club.banyuan.hrms.dao.AdminDao" %>
<%@ page import="club.banyuan.hrms.dao.impl.AdminDaoImpl" %>
<%@ page import="club.banyuan.hrms.domain.Admin" %>
<%@ page import="club.banyuan.hrms.dao.impl.AdminDbDaoImpl" %>
<%@ page import="org.springframework.web.context.support.WebApplicationContextUtils" %>
<%@ page import="org.springframework.web.context.WebApplicationContext" %>
<%@ page language="java" contentType="text/html; charset=UTF-8"
         pageEncoding="UTF-8" %>
<%@ page isELIgnored="false" %>
<!doctype html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport"
          content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <link rel="stylesheet" type="text/css"
          href="static/jquery-easyui-1.3.3/themes/default/easyui.css">
    <link rel="stylesheet" type="text/css"
          href="static/jquery-easyui-1.3.3/themes/icon.css">
    <link rel="icon" href="">
    <script type="text/javascript"
            src="static/jquery-easyui-1.3.3/jquery.min.js"></script>
    <script type="text/javascript"
            src="static/jquery-easyui-1.3.3/jquery.easyui.min.js"></script>
    <script type="text/javascript"
            src="static/js/common.js"></script>
    <script type="text/javascript">
        var url;

        function searchAdmin() {
            $("#dg").datagrid('load', {
                "username": $("#s_username").val()
            });
        }

        function deleteAdmin() {
            var selectedRows = $("#dg").datagrid('getSelections');
            if (selectedRows.length == 0) {
                $.messager.alert("system prompt",
                    "Please choose the data to delete!");
                return;
            }

            var strIds = [];
            for (var i = 0; i < selectedRows.length; i++) {
                if (selectedRows[i].id === 1) {
                    $.messager.alert("system prompt",
                        "Can't modity superadmin' information!");
                    return;
                }
                strIds.push(selectedRows[i].id);
            }
            var ids = strIds.join(",");
            $.messager
                .confirm(
                    "system prompt",
                    "Do you want to delete the <font color=red>"
                    + selectedRows.length + "</font> data?",
                    function (r) {
                        if (r) {
                            $
                                .post(
                                    "admin/delete",
                                    {
                                        ids: ids
                                    },
                                    function (result) {
                                        if (result.code !== 0) {
                                            $.messager.alert("system prompt", result.message);
                                        } else {
                                            $.messager.alert("system prompt", "Delete successful!");
                                            $("#dg").datagrid("reload");
                                        }
                                    }, "json");
                        }
                    });
        }

        function openAdminAddDialog() {
            $("#dlg").dialog("open").dialog("setTitle", "Add new admin");
            url = "admin/save";
        }

        function saveAdmin() {
            $("#fm").form("submit", {
                url: url,
                onSubmit: function () {
                    return $(this).form("validate");
                },
                success: function (result) {
                    // 表单提交需要转json，post不需要转
                    result = JSON.parse(result);
                    if (result.code !== 0) {
                        $.messager.alert("system prompt", result.message);
                    } else {
                        $.messager.alert("system prompt", "Save successful!");
                        resetValue();
                        $("#dlg").dialog("close");
                        $("#dg").datagrid("reload");
                    }
                }
            });
        }

        function openAdminModifyDialog() {
            var selectedRows = $("#dg").datagrid('getSelections');
            if (selectedRows.length != 1) {
                $.messager.alert("system prompt", "Please choose a data to edit!");
                return;
            }
            var row = selectedRows[0];
            if (row.id == 1) {
                $.messager.alert("system prompt",
                    "Can't modity superadmin' information!");
                return;
            }
            $("#dlg").dialog("open").dialog("setTitle", "Edit admin information");
            $('#fm').form('load', row);
            $("#password").val("******");
            url = "admin/save?id=" + row.id;
        }

        function resetValue() {
            $("#username").val("");
            $("#password").val("");
        }

        function closeAdminDialog() {
            $("#dlg").dialog("close");
            resetValue();
        }
    </script>
</head>
<body style="margin: 1px;">

<div style="width: 620px; height: 250px; padding: 10px 20px" closed="true">
<%--    <form id="fm" method="post" action="admin/list">--%>
    <form id="fm" method="post" action="admins">
        <% Integer id = (Integer) request.getAttribute("id");
//    Admin admin = (Admin) request.getAttribute("admin");
//    String username = "";
//    String rolename = "";
//    String password = "";
////    String password = (String) request.getAttribute("password");
////      if (password == null){
////        password = "";
////      }
//
//      if (admin!=null){
//        username = admin.getUsername();
//        rolename = admin.getRolename();
//        password = admin.getPassword();
//      }
            if (id != null) {
        %>
        <%--  隐藏input域  --%>
        <input name="id" value="${id}" hidden="true"/>
        <% } %>
        <table cellspacing="8px">
            <tr>
                <td>Username:</td>
                <td><input type="text" id="username" name="username" value="${username}"
                           class="easyui-validatebox" required="true"/>&nbsp;<font
                        color="red">*</font></td>
            </tr>
            <td>Rolename:</td>
            <td><input type="text" id="rolename" name="rolename" value="${rolename}"
                       class="easyui-validatebox" required="true"/>&nbsp;<font
                    color="red">*</font></td>
            </tr>
            <tr>
                <td>Password:</td>
                <td><input type="text" id="password" name="password" value="${password}"
                           class="easyui-validatebox" required="true"/>&nbsp;<font
                        color="red">*</font></td>
            </tr>
            <tr>
                <td><input type="submit" name="ok" value="Save"/>&nbsp;
                <td>
                    <button type="button" name="back" value="Back" id="back" onClick="javascript:history.back(-1)">
                        Back
                    </button>
            </tr>
        </table>
    </form>
</div>
</body>
</html>